<?php
Class Database {
	private $link;
	private $res;
	private $error;
	public $charset = "utf8";
	
	function __construct($db_host,$db_user,$db_pass,$db_name){
		$this->link = mysql_connect($db_host,$db_user,$db_pass);
		if($this->link===false){
			Messages::error("Проблем със свързването към базата данни:  ".$this->error());
			exit();
		}
		if(!mysql_select_db($db_name,$this->link)){
			Messages::error("Проблем със името на базата данни!");
		}
		mysql_set_charset($this->charset);
	}

	function query($qstring){
		$this->res = @mysql_query($qstring);
		$this->error = $this->error();
		if($this->error!=""){
			die(Messages::error("Проблем със заявката към базата данни: ".$this->error."<br />"));
		}
		return $this->res;
	}
	function fetch_assoc($qstring){
		$this->res = @mysql_fetch_assoc($qstring);
		$this->error = $this->error();
		if($this->error!=""){
			Messages::error($this->error."<br />");
		}
		return $this->res;
	}
	function fetch_array($qstring){
		$this->res = @mysql_fetch_array($qstring);
		$this->error = $this->error();
		if($this->error!=""){
			Messages::error($this->error."<br />");
		}
		return $this->res;
	}
	function num_rows($qstring){
		$this->res = @mysql_num_rows($qstring);
		$this->error = $this->error();
		if($this->error != ""){
			Messages::error($this->error."<br />");
		}
		return $this->res;
	}
	function error(){
		return mysql_error($this->link);
	}
	function __destruct(){
		mysql_close($this->link);
	}
}